﻿package plugin.fcactivity
{
    import cmd.*;
    import com.core.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import pluginfw.nio.*;
    import uiresource.activity.*;
    import view.mornExtends.*;
    import vo.*;

    public class FCActivityGame extends CFActivityUI
    {
        private var _instance:FCActivity;
        private var _timeDic:Dictionary;
        private var tabArr:Array;
        private var selectIndex:int = 0;
        private var selectState:Array;

        public function FCActivityGame(param1:FCActivity)
        {
            this._timeDic = new Dictionary();
            this.tabArr = [{type:9, img:"cjhl"}, {type:10, img:"drhl"}];
            this._instance = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            var _loc_2:Object = null;
            var _loc_3:Control = null;
            var _loc_1:* = ConfigDictionary.data.activitytime;
            for each (_loc_2 in _loc_1)
            {
                
                if (_loc_2.activetype == 2)
                {
                    this._timeDic[_loc_2.id] = _loc_2;
                }
            }
            list_tab.array = this.tabArr;
            list_tab.renderHandler = new Handler(this.tabRender);
            list_tab.selectedIndex = this.selectIndex;
            this.initEvent();
            _loc_3 = Control.create(FCAtivityControl, this);
            ModelProxy.addObserver("fcactivity", _loc_3);
            return;
        }// end function

        private function tabRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Image = null;
            var _loc_5:Image = null;
            var _loc_6:Button = null;
            if (param2 < list_tab.length)
            {
                _loc_3 = param1.dataSource;
                _loc_4 = param1.getChildByName("img_label") as Image;
                _loc_4.url = "kfjj/" + _loc_3.img + ".png";
                _loc_5 = param1.getChildByName("img_lq") as Image;
                _loc_5.visible = false;
                this.checkOneAngle(_loc_5, param2);
                _loc_6 = param1.getChildByName("btn") as Button;
                if (this.selectIndex == param2)
                {
                    _loc_6.selected = true;
                    this.showSelectMsg(_loc_3.type);
                }
                else
                {
                    _loc_6.selected = false;
                }
            }
            return;
        }// end function

        public function checkOneAngle(param1:Image, param2:int) : void
        {
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            var _loc_8:Object = null;
            var _loc_3:* = ModelProxy.getValue("data", "jingjitypedata");
            if (_loc_3)
            {
                _loc_4 = this.tabArr[param2];
                _loc_5 = _loc_3[_loc_4.type];
                if (_loc_5)
                {
                    _loc_6 = ConfigDictionary.data.activitygoal[_loc_4.type];
                    _loc_7 = 0;
                    while (_loc_7 < _loc_6.length)
                    {
                        
                        _loc_8 = _loc_6[_loc_7];
                        if (FCActivityUtil.checkLimit(_loc_8.type, _loc_8.goal))
                        {
                            if (_loc_5[0][_loc_7].isGet == 0)
                            {
                                param1.visible = true;
                                break;
                            }
                        }
                        _loc_7++;
                    }
                }
            }
            return;
        }// end function

        public function checkAllAngle() : void
        {
            var _loc_2:Component = null;
            var _loc_3:Image = null;
            var _loc_1:int = 0;
            while (_loc_1 < this.tabArr.length)
            {
                
                _loc_2 = list_tab.getCell(_loc_1);
                _loc_3 = _loc_2.getChildByName("img_lq") as Image;
                _loc_3.visible = false;
                this.checkOneAngle(_loc_3, _loc_1);
                _loc_1++;
            }
            return;
        }// end function

        public function showSelectMsg(param1:int) : void
        {
            var _loc_2:* = this._timeDic[param1];
            if (_loc_2.timetype == 1)
            {
                txt_time.text = this.getTime(_loc_2.opentime, _loc_2.overtime);
            }
            txt_desc.text = _loc_2.shuoming;
            img_title.url = "kfjj/" + this.tabArr[this.selectIndex].img + "1.png";
            var _loc_3:* = ModelProxy.getValue("data", "jingjitypedata");
            this.selectState = null;
            if (_loc_3)
            {
                if (_loc_3[param1])
                {
                    this.selectState = _loc_3[param1][0];
                }
            }
            var _loc_4:* = ConfigDictionary.data.activitygoal[param1];
            list.array = _loc_4;
            list.renderHandler = new Handler(this.listRender);
            return;
        }// end function

        private function listRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Box = null;
            var _loc_5:Box = null;
            var _loc_6:Image = null;
            var _loc_7:Box = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:String = null;
            var _loc_11:Image = null;
            var _loc_12:List = null;
            var _loc_13:Button = null;
            var _loc_14:Box = null;
            if (param2 < list.length)
            {
                _loc_3 = param1.dataSource;
                _loc_4 = param1.getChildByName("box_level") as Box;
                _loc_5 = param1.getChildByName("box_day") as Box;
                _loc_9 = 0;
                switch(int(_loc_3.type))
                {
                    case 8:
                    {
                        _loc_5.visible = false;
                        _loc_4.visible = true;
                        _loc_6 = _loc_4.getChildByName("img_type") as Image;
                        _loc_6.url = "kfjj/daod.png";
                        _loc_7 = _loc_4.getChildByName("boxforimg_level") as Box;
                        while (_loc_7.numChildren > 0)
                        {
                            
                            _loc_7.removeChildAt(0);
                        }
                        _loc_8 = String(_loc_3.goal).length;
                        _loc_9 = 0;
                        while (_loc_9 < _loc_8)
                        {
                            
                            _loc_10 = String(_loc_3.goal).charAt(_loc_9);
                            _loc_11 = new Image("png.common.pow2_" + _loc_10);
                            _loc_7.addChild(_loc_11);
                            _loc_11.x = _loc_9 * 25;
                            _loc_9++;
                        }
                        if (_loc_8 == 1)
                        {
                            _loc_7.x = 130;
                        }
                        else if (_loc_8 == 2)
                        {
                            _loc_7.x = 114;
                        }
                        else
                        {
                            _loc_7.x = 105;
                        }
                        break;
                    }
                    case 9:
                    {
                        _loc_5.visible = false;
                        _loc_4.visible = true;
                        _loc_6 = _loc_4.getChildByName("img_type") as Image;
                        _loc_6.url = "kfjj/dad.png";
                        _loc_7 = _loc_4.getChildByName("boxforimg_level") as Box;
                        while (_loc_7.numChildren > 0)
                        {
                            
                            _loc_7.removeChildAt(0);
                        }
                        _loc_8 = String(_loc_3.goal).length;
                        _loc_9 = 0;
                        while (_loc_9 < _loc_8)
                        {
                            
                            _loc_10 = String(_loc_3.goal).charAt(_loc_9);
                            _loc_11 = new Image("png.common.pow2_" + _loc_10);
                            _loc_7.addChild(_loc_11);
                            _loc_11.x = _loc_9 * 25;
                            _loc_9++;
                        }
                        if (_loc_8 == 1)
                        {
                            _loc_7.x = 130;
                        }
                        else if (_loc_8 == 2)
                        {
                            _loc_7.x = 114;
                        }
                        else
                        {
                            _loc_7.x = 105;
                        }
                        break;
                    }
                    case 10:
                    {
                        _loc_5.visible = true;
                        _loc_4.visible = false;
                        _loc_14 = _loc_5.getChildByName("boxforimg_day") as Box;
                        while (_loc_14.numChildren > 0)
                        {
                            
                            _loc_14.removeChildAt(0);
                        }
                        _loc_8 = String(_loc_3.goal).length;
                        _loc_9 = 0;
                        while (_loc_9 < _loc_8)
                        {
                            
                            _loc_10 = String(_loc_3.goal).charAt(_loc_9);
                            _loc_11 = new Image("png.common.pow2_" + _loc_10);
                            _loc_14.addChild(_loc_11);
                            _loc_11.x = _loc_9 * 20;
                            _loc_9++;
                        }
                        if (_loc_8 == 1)
                        {
                            _loc_14.x = 105;
                        }
                        else if (_loc_8 == 2)
                        {
                            _loc_14.x = 99;
                        }
                        else
                        {
                            _loc_14.x = 93;
                        }
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                _loc_12 = param1.getChildByName("list_goods") as List;
                _loc_12.array = this.getBoxGoods(_loc_3.reward);
                _loc_12.renderHandler = new Handler(this.goodsRender, [_loc_12]);
                _loc_13 = param1.getChildByName("btn_lq") as Button;
                if (FCActivityUtil.checkLimit(_loc_3.type, _loc_3.goal))
                {
                    if (this.selectState)
                    {
                        if (this.selectState[param2].isGet == 0)
                        {
                            _loc_13.disabled = false;
                            _loc_13.label = "领取";
                            _loc_13.clickHandler = new Handler(this.dolq, [_loc_3.id, _loc_3.goal, _loc_3.type]);
                        }
                        else
                        {
                            _loc_13.disabled = true;
                            _loc_13.label = "已领取";
                        }
                    }
                    else
                    {
                        _loc_13.disabled = true;
                        _loc_13.label = "领取";
                    }
                }
                else
                {
                    _loc_13.disabled = true;
                    _loc_13.label = "领取";
                }
            }
            return;
        }// end function

        private function dolq(param1:int, param2:int, param3:int) : void
        {
            var _loc_4:* = new Output();
            new Output().writeVarInt(param1, false);
            _loc_4.writeVarInt(param2, false);
            _loc_4.writeVarInt(param3, false);
            PluginManager.instance.sendToBufferServer("kaifujingji", CMDKaifuJJ.SendgoalLJMassage, _loc_4);
            return;
        }// end function

        private function goodsRender(param1:List, param2:Component, param3:int) : void
        {
            if (param3 < param1.length)
            {
                (param2 as FlashItem).data = param2.dataSource;
            }
            return;
        }// end function

        private function getBoxGoods(param1:String) : Array
        {
            var _loc_7:String = null;
            var _loc_8:Array = null;
            var _loc_9:Object = null;
            var _loc_2:* = ModelProxy.getValue("data", "configs.box");
            var _loc_3:* = ModelProxy.getValue("data", "configs.item");
            var _loc_4:Array = [];
            var _loc_5:* = (_loc_2[param1].items as String).split(";");
            var _loc_6:int = 0;
            while (_loc_6 < _loc_5.length)
            {
                
                _loc_7 = _loc_5[_loc_6];
                _loc_8 = _loc_7.split("-");
                if (_loc_8.length >= 4)
                {
                    if (_loc_8[3] != 2 && _loc_8[3] != ModelProxy.getValue("data", "user.sex"))
                    {
                        ;
                    }
                }
                if (_loc_8.length >= 5)
                {
                    if (_loc_8[4] != 0 && _loc_8[4] != ModelProxy.getValue("data", "user.career"))
                    {
                        ;
                    }
                }
                _loc_9 = ItemObject.create(_loc_3[_loc_8[0]]);
                _loc_9.count = _loc_8[1];
                _loc_4.push(_loc_9);
                _loc_6++;
            }
            return _loc_4;
        }// end function

        public function getTime(param1:int, param2:int) : String
        {
            var _loc_3:* = int(ModelProxy.getValue("data", "openday")) + 1;
            var _loc_4:* = Number(ModelProxy.getValue("data", "servertime") + getTimer()) / 1000;
            var _loc_5:* = new Date();
            new Date().setTime(_loc_4 * 1000);
            var _loc_6:* = new Date(_loc_5.fullYear, _loc_5.month, _loc_5.date, 23, 59, 59, 59);
            var _loc_7:* = new Date();
            new Date().setTime(_loc_6.getTime() - (_loc_3 - param1) * 86400000);
            var _loc_8:* = _loc_7.getFullYear();
            var _loc_9:* = _loc_7.getMonth() + 1;
            var _loc_10:* = _loc_7.getDate();
            var _loc_11:* = new Date();
            new Date().setTime(_loc_6.getTime() + Number(param2 - _loc_3) * 86400000);
            var _loc_12:* = _loc_11.getFullYear();
            var _loc_13:* = _loc_11.getMonth() + 1;
            var _loc_14:* = _loc_11.getDate();
            return _loc_8 + "年" + _loc_9 + "月" + _loc_10 + "日-" + _loc_12 + "年" + _loc_13 + "月" + _loc_14 + "日";
        }// end function

        private function initEvent() : void
        {
            btn_close.clickHandler = new Handler(this._instance.close);
            list_tab.selectHandler = new Handler(this.tabSelect);
            return;
        }// end function

        private function tabSelect(param1:int) : void
        {
            var _loc_3:Component = null;
            var _loc_4:Button = null;
            this.selectIndex = param1;
            var _loc_2:int = 0;
            while (_loc_2 < list_tab.cells.length)
            {
                
                _loc_3 = list_tab.cells[_loc_2];
                _loc_4 = _loc_3.getChildByName("btn") as Button;
                _loc_4.selected = _loc_2 == param1;
                _loc_2++;
            }
            this.showSelectMsg(this.tabArr[this.selectIndex].type);
            return;
        }// end function

        public function update(param1:int, param2:Object) : void
        {
            if (this.visible == false)
            {
                return;
            }
            switch(param1)
            {
                case CmdFcActivity.REFRESH:
                {
                    this.showSelectMsg(this.tabArr[this.selectIndex].type);
                    this.checkAllAngle();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

    }
}
